// pages/mine/mine.js
Page({
  data: {
    menuList: [
      [{ title: '修改设备名称', icon: 'success', iconColor: '#dd6161' }],
      [{ title: '修改范围大小', icon: 'warn', iconColor: '#f29100' }],
      [{ title: '设备详情', icon: 'download', iconColor: 'purple' }]
    ]
  },

  menuClick: function (e) {
    let item = e.currentTarget.dataset.item;
    if (item.title === '修改范围大小') {
      this.showRadiusInput();
    } else if (item.title === '设备详情') {
      wx.navigateTo({
        url: '/pages/deviceDetail/deviceDetail'
      });
    } else if (item.title === '修改设备名称') {
      this.showDeviceNameInput();
    }
  },

  showRadiusInput: function () {
    wx.showModal({
      title: '修改范围大小（米）',
      // content: '请输入新的范围大小',
      showCancel: true,
      editable: true,
      success: (res) => {
        if (res.confirm) {
          const newRadius = parseFloat(res.content);
          if (isNaN(newRadius) || newRadius <= 0) {
            wx.showToast({
              title: '输入无效',
              icon: 'none',
              duration: 2000
            });
          } else {
            getApp().setCircleRadius(newRadius); // 保存新的范围大小到全局变量
            wx.navigateBack({
              delta: 1,
              success: () => {
                wx.showToast({
                  title: '范围大小已修改',
                  icon: 'success',
                  duration: 2000
                });
              }
            });
          }
        }
      }
    });
  },

  showDeviceNameInput: function () {
    wx.showModal({
      title: '修改设备名称',
      showCancel: true,
      editable: true,
      success: (res) => {
        if (res.confirm) {
          const newDeviceName = res.content;
          getApp().setDeviceName(newDeviceName); // 保存新的设备名称
          wx.navigateBack({
            delta: 1,
            success: () => {
              wx.showToast({
                title: '设备名称已修改',
                icon: 'success',
                duration: 2000
              });
            }
          });
        }
      }
    });
  }
});